package persistent.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

import basic.HibBaseDAO;
import basic.HibernateSessionFactory;
import model.TCloudSubject;
import model.TCloudUser;
import model.TTeacherInfo;
import model.VCloudSchoolRole;
import model.VCloudTeachRole;
import model.VTeacherInfo;
import persistent.dao.TeacherInfoDAO;
import util.toolkit.Constant;
import util.toolkit.LogUtil;

/**
 * 教师管理业务类接口的实现
 * @author djf
 * @since 2018/10/6
 */
public class TeacherInfoImpl implements TeacherInfoDAO {
	private HibBaseDAO dao=null;
	
	private static final Log log = LogFactory.getLog(ParentInfoImpl.class);
	
	public TeacherInfoImpl() {
		dao=new HibBaseDAO();
	}

	/**
	 * 添加一个教师
	 * @param teacher 教师对象
	 * @return 成功返回主键，失败返回0
	 */ 
	@Override
	public int addTecher(TTeacherInfo teacher) {
		return dao.insert(teacher);		
		/*Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		int row1 = 0;
		try {
			
			TCloudUser user = new TCloudUser();
			user.setPhone(teacher.getPhone());
			user.setRoleid(teacher.getRoleid());
			user.setStatus("停用");
			
			Serializable key = session.save(user);// 先保存用户信息表
			int row = ((Integer) key).intValue();// 得到自动编号
			
			if(row > 0){
				teacher.setUserid(row);
				Serializable key1 = session.save(teacher);
				row1 = ((Integer) key1).intValue();
			}
			tx.commit();
			session.close();//关闭连接
			return row1;
		} catch (Exception e) {
			if(tx != null) tx.rollback();
			session.close(); //关闭连接
			log.error("persistent.impl包TeacherInfoImpl类addTecher(TTeacherInfo teacher)方法调用错误：");
			log.error(LogUtil.getMsg(e));
			System.out.println("persistent.impl包TeacherInfoImpl类addTecher(TTeacherInfo teacher)方法调用错误：");
			e.printStackTrace();
		}
		return 0;*/
	}

	/**
	 * 修改教师信息
	 * @param teacher
	 * @return 修改成功返回true，失败返回false
	 */
	@Override
	public boolean updateTeacher(TTeacherInfo teacher) {
		TTeacherInfo teacher2=(TTeacherInfo)dao.findById(TTeacherInfo.class, teacher.getTeainfoid());
		teacher2.setUserName(teacher.getUserName());
		teacher2.setRealName(teacher.getRealName());
		teacher2.setSex(teacher.getSex());
		teacher2.setBirthday(teacher.getBirthday());
		teacher2.setEmail(teacher.getEmail());
		teacher2.setPhone(teacher.getPhone());
		teacher2.setTell(teacher.getTell());
		teacher2.setWorkTel(teacher.getWorkTel());
		teacher2.setAddress(teacher.getAddress());
		teacher2.setPost(teacher.getPost());
		teacher2.setGraduateSchool(teacher.getGraduateSchool());
		teacher2.setEntryTime(teacher.getEntryTime());
		teacher2.setActiveState(teacher.getActiveState());
		teacher2.setRoleid(teacher.getRoleid());
		teacher2.setSchoolid(teacher.getSchoolid());
		return dao.update(teacher2);
	}
	
	/**
	 * 删除一个教师信息
	 * @param teaid 教师id
	 * @return 修改成功返回true，失败返回false
	 * @since 2018-11-24 10:16
	 */
	@Override
	public boolean deleteTeacher(int teaid) {
		
		TTeacherInfo teainfo = (TTeacherInfo) dao.findById(
				TTeacherInfo.class, teaid);
		String hql = "from TCloudUser where phone = ?";
		Object[] para = { teainfo.getPhone() };
		List<TCloudUser> userlist = dao.select(hql, para);
		if(userlist!=null && userlist.size()>0){
			return false;
		}
		return dao.delete(teainfo);
	}

	/**
	 * 根据教师id得到一个教师信息
	 * @param teainfoid 教师id
	 * @return 成功返回TTeacherInfo教师对象，失败返回null
	 */
	@Override
	public TTeacherInfo getTeacher(int teainfoid) {
		return (TTeacherInfo)dao.findById(TTeacherInfo.class, teainfoid);
	}

	/**
	 * 通过查询条件过的一个教师信息列表
	 * @param strwhere 查询条件
	 * @return 成功返回TTeacherInfo类型的列表，失败返回null
	 */
	@Override
	public List<VTeacherInfo> getTeaListBywhere(String strwhere) {
		String hql="from VTeacherInfo "+strwhere;
		return dao.select(hql);
	}

	/**
	 * 通过查询条件获得一个带分页的教师信息列表
	 * @param strwhere 查询条件
	 * @return 成功返回TTeacherInfo列表，失败返回null
	 */
	@Override
	public List<VTeacherInfo> getTeaListByPage(String strwhere,int currentPage, int pageSize) {
		String hql="from VTeacherInfo "+strwhere;
		return dao.selectByPage(hql, currentPage, pageSize);
	}

	/**
	 * 批量导入教师信息 
	 * @param list 教师信息list
	 * @return 成功返回true，失败返回false
	 */
	@Override
	public List<TTeacherInfo> batchImport(List<TTeacherInfo> teaList) {

		List<TTeacherInfo> failList = new ArrayList<TTeacherInfo>();
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		
		for (int i = 0; i < teaList.size(); i++) {
			try {
				TTeacherInfo teacher = teaList.get(i);
				//TCloudUser user = new TCloudUser();
				//user.setPhone(teacher.getPhone());
				//user.setRoleid(teacher.getRoleid());
				//user.setStatus("停用");

				//Serializable key = session.save(user);// 先保存用户信息表
				//int row = ((Integer) key).intValue();// 得到自动编号

				//if (row > 0) {
				//	teacher.setUserid(row);
					
				//}
				session.save(teacher);
			} catch (Exception e) {
				failList.add(teaList.get(i));
				log.error("persistent.impl包TeacherInfoImpl类batchImport(List<TTeacherInfo> teaList)方法调用错误：");
				log.error(LogUtil.getMsg(e));
				System.out.println("persistent.impl包TeacherInfoImpl类batchImport(List<TTeacherInfo> teaList)方法调用错误：");
				e.printStackTrace();
			}
		}
		if(tx != null && failList.size() == 0){
			tx.commit();
			session.close(); //关闭连接
		}else if(tx != null && failList.size() > 0){
			tx.rollback();
			session.close(); //关闭连接
		}
		return failList;
	}

	/**
	 * 激活教师信息
	 * @param phone 手机号
	 * @param userName 用户名
	 * @param pwd 密码
	 * @return 成功返回true，失败返回false
	 */
	@Override
	public boolean teaActivationaCcount(String phone, String userName,
			String pwd) {
		String hql = "from TTeacherInfo where phone = ?";
		Object[] para = { phone };
		List<TTeacherInfo> teaList = dao.select(hql, para);
		TCloudUser user = new TCloudUser();
		if (teaList != null && teaList.size() > 0) {
			user = (TCloudUser) dao.findById(TCloudUser.class, teaList.get(0)
					.getUserid());
			user.setPwd(pwd);
			user.setUserName(userName);
			user.setStatus(Constant.ENABLE);
			boolean row = dao.update(user);
			if (row) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

	/**
	 * 给一个教师添加班级
	 * @param roleid 教师权限id
	 * @param teaid 教师id
	 * @param subid 所带科目id
	 * @param classid 班级id
	 * @return 成功返回1,失败返回0
	 */
	@Override
	public int addTeacherRole(int roleid, int teaid, int subid, String classid) {
		String procname="up_cloud_add_teach_role(?,?,?,?)";
		Object para[]={roleid,teaid,subid,classid};
		int row=(Integer)dao.executeProduce(procname, para);
		return row;
	}

	/**
	 * 删除一个教师任课班级
	 * @param teachid 教师id
	 * @return 成功返回int,失败返回0
	 */
	@Override
	public int deleteTeacherRole(int teachid) {
		String procname="up_cloud_delete_teach_role(?)";
		Object para[]={teachid};
		int row=(Integer)dao.executeProduce(procname, para);
		return row;
	}

	/**
	 * 得到一个不分页的教师任课班级列表
	 * @param strwhere 查询条件
	 * @return 成功返回VCloudTeachRole对象列表，失败返回null
	 */
	@Override
	public List<VCloudTeachRole> getTeacherRole(String strwhere) {
		String hql="from VCloudTeachRole "+strwhere;
		return dao.select(hql);
	}

	/**
	 * 得到一个带分页的教师任课班级列表
	 * @param strwhere
	 * @param currentPage
	 * @param pageSize
	 * @return 成功返回VCloudTeachRole对象列表，失败返回null
	 */
	@Override
	public List<VCloudTeachRole> getTeachRoleByPage(String strwhere,
			int currentPage, int pageSize) {
		String hql="from VCloudTeachRole "+strwhere;
		return dao.selectByPage(hql, currentPage, pageSize);
	}

	/**
	 * 获取所有的教师职务
	 * @return TCloudSubject
	 */
	@Override
	public List<TCloudSubject> getAllCloudSubject() {
		String hql="from TCloudSubject";
		return dao.select(hql);
	}

	/**
	 * 根据查询条件查询得到教师表中的所有角色权限
	 * @param strwhere 查询条件
	 * @return 成功返回VCloudSchoolRole类型的列表，是失败返回null
	 */
	@Override
	public List<VCloudSchoolRole> getSchoolRole(String strwhere) {
		String hql="from VCloudSchoolRole "+strwhere;
		return dao.select(hql);
	}

}
